(*this is the old version of MMADE, it's unsupported*)
(*******************************************************************
This file was generated automatically by the Mathematica front end.
It contains Initialization cells from a Notebook file, which
typically will have the same name as this file except ending in
".nb" instead of ".m".

This file is intended to be loaded into the Mathematica kernel using
the package loading commands Get or Needs.  Doing so is equivalent
to using the Evaluate Initialization Cells menu command in the front
end.

DO NOT EDIT THIS FILE.  This entire file is regenerated
automatically each time the parent Notebook file is saved in the
Mathematica front end.  Any changes you make to this file will be
overwritten.
***********************************************************************)

BeginPackage["exDBML`"];

Needs["Utilities`FilterOptions`"]

exMath::usage="exMath[name,expr,type,title] exports a Docbook encapsulated MathML file into the present working directory with name as a base name string, type as a base string to indicate the type of docbook equation element, and title to specify the title of the element. The type must be one of \"\" (equation) \"informal\" (informalequation) \"inline\" (inlineequation). The title must be an empty string for a non-formal equation. The title must be a string of nonzero length for a formal equation. As an example: exMath[\"fun\",1-y=x,\"\",\"fun equation\"] would export a file called fun-eqns.xml containing the profiled MathML, picture, and alt-text DocBook markup, fun.png containing a picture of 1-y=x, and fun-xrefs.xml containing two (profiled) xrefs to the two (profiled) equations in fun-eqns.xml." 

GraphicMathForm::usage=
  "The option whose setting is a function operating on the expression to be exported as a raster picture (usually.png). The default value is TraditionalForm."

GraphicsQ::usage=
  "GraphicsQ[grph] yeilds true if grph is non empty and has a type of graphics as its head"

exGraph::usage=
  "exGraph[name,expr,type,title,description] exports a DocBook graphic."

exTable::usage=
  "exTable[name,table,title] exports a DocBook compatible CALS Model Table"

ExportWithId::usage=
  "ExportWithId is an option for exMath that, if True, causes an id=name (with appropriate mathml or non-mathml suffix) attribute to be added to the exported equation elements"

literallayout::usage=
  "literallayout[string] corresponds to the eponymous element in DocBook, allowing one to format strings with explicit line breaks"

Begin["`Private`"];

publicId="-//OASIS//DTD DocBook MathML Module V1.1b1//EN";
systemId="http://www.oasis-open.org/docbook/xml/mathml/1.1CR1/dbmathml.dtd";

Options[exMath]={GraphicMathForm\[Rule]TraditionalForm,
    ImageResolution\[Rule]86,ExportWithId\[Rule]True}
exMath[name_String,expr_,type_String,
    title_/;MatchQ[Head[title],(String|RowBox|literallayout)],
    opts___?OptionQ]:=
  Module[{parenttag=
        Which[type=="","para",type=="informal","para",type=="inline","phrase",
          True,Abort[]],
      equationtag=
        Which[type=="","equation",type=="informal","informalequation",type=="inline",
          "inlineequation",True,Abort[]],
      mediatag=Which[type=="","mediaobject",type=="informal","mediaobject",
          type=="inline","inlinemediaobject",True,Abort[]],titletag,
      exportwithid=ExportWithId/.{opts}/.Options[exMath]},
    titletag=If[equationtag==="equation",titleML[name,title],""];
    If[title===type==="",
      Abort[],{Export[StringJoin[name,"-eqns.xml"],
          StringReplace[
            ExportString[
              XMLObject[
                  "Document"][{XMLObject["Declaration"]["Version"->"1.0",
                    "Encoding"->"UTF-8"],
                  XMLObject["Doctype"][parenttag,"Public"->publicId,
                    "System"->systemId]},
                XMLElement[
                  parenttag,{},{XMLElement[
                      equationtag,{"condition"->"mathml",
                        If[exportwithid,"id"->StringJoin[name,"-mathml"],
                          Unevaluated[Sequence[]]]},{titletag,
                        XML`RawXML[
                          StringReplace[
                            ExportString[expr,"MathML",
                              ConversionOptions->{"Formats"->{"PresentationMathML",
                                      "ContentMathML"},
                                  "NamespacePrefixes"->{"http://www.w3.org/1998/Math/MathML"->
                                        "mml"},
                                  "ElementFormatting"\[Rule]
                                    None}],{StringExpression[Whitespace,
                                  "xmlns='http://www.w3.org/1998/Math/MathML'"]\
->"","&#63449;"->"=","&#62305;"->"{"}]]}],
                    XMLElement[
                      equationtag,{"condition"->"nonmathml",
                        If[exportwithid,"id"->StringJoin[name,"-nonmathml"],
                          Unevaluated[Sequence[]]]},{titletag,
                        XMLElement[
                          mediatag,{},{(*XMLElement[
                                "imageobject",{},{XMLElement[
                                    "imagedata",{"fileref"->
                                        Export[StringJoin[name,".eps"],
                                        GraphicMathForm[
                                        expr]/.{opts}/.Options[
                                        exMath]]},{}]}],*)XMLElement[
                              "imageobject",{},{XMLElement[
                                  "imagedata",{"fileref"->
                                      Export[StringJoin[name,".png"],
                                        GraphicMathForm[
                                        expr]/.{opts}/.Options[exMath],
                                        Rule[ImageResolution,
                                        ImageResolution/.{opts}/.Options[
                                        exMath]]]},{}]}],
                            XMLElement[
                              "textobject",{},{XMLElement[
                                  "phrase",{},{ExportString[InputForm[expr],
                                      "Text"]}]}]}]}]}],{},"Valid"->True],
              "XML"],{"&#8290;"->
                "&#8290;"(*this arrangement was caused by Firefox 1.5.0.2 - 
                  the rhs originally had a blank space, 
                but that doesn't actually help the problem*)}],"Text"],
        If[exportwithid,
          Export[StringJoin[name,"-xrefs.xml"],
            XMLObject[
                "Document"][{XMLObject["Declaration"]["Version"->"1.0",
                  "Encoding"->"UTF-8"],
                XMLObject["Doctype"][parenttag,"Public"->publicId,
                  "System"->systemId]},
              XMLElement[
                "phrase",{},{XMLElement[
                    "xref",{"condition"->"mathml",
                      "linkend"->StringJoin[name,"-mathml"]},{}],
                  XMLElement[
                    "xref",{"condition"->"nonmathml",
                      "linkend"->StringJoin[name,"-nonmathml"]},{}]}],{},
              "Valid"->True],"XML"],Unevaluated[Sequence[]]]}]]

GraphicsQ[grph_]:=
  MatchQ[grph,#[__]&/@
      Alternatives[Graphics,Graphics3D,SurfaceGraphics,ContourGraphics,
        DensityGraphics]]

Options[exGraph]={ImageResolution\[Rule]86,"AnimationDisplayTime"\[Rule]0}
exGraph[name_String,expr:Alternatives[{__?GraphicsQ},_?GraphicsQ],type_String,
    title_/;MatchQ[Head[title],(String|RowBox|literallayout)],
    description_String,opts___?OptionQ]:=
  Module[{mediaobjectelement,mediaobjecttag,mediaobjectattributes,figuretag,
      parentelement,parenttag},
    mediaobjectelement=
      XMLElement[mediaobjecttag, 
        mediaobjectattributes, {XMLElement[
            "imageobject", {}, {XMLElement[
                "imagedata", {"fileref"\[Rule]
                    If[Head[expr]===List,
                      Export[StringJoin[name,".gif"],expr,
                        Rule[ImageResolution,
                          ImageResolution/.{opts}/.Options[exGraph]],
                        ConversionOptions\[Rule]{Rule["AnimationDisplayTime",
                              "AnimationDisplayTime"/.{opts}/.Options[
                                  exGraph]],"Loop"\[Rule]True,
                            "GlobalColorReduction"\[Rule]False}],
                      Export[StringJoin[name,".png"],expr,
                        Rule[ImageResolution,
                          ImageResolution/.{opts}/.Options[
                              exGraph]]]]}, {}]}],
          XMLElement[
            "textobject",{},{XMLElement["phrase",{},{description}]}]}];
    parentelement=
      Which[And[type==="",title=!="",
          description=!=""],{figuretag,mediaobjecttag,
            mediaobjectattributes}={"figure","mediaobject",{}};
        parenttag=figuretag;
        XMLElement[
          parenttag,{"id"->name},{titleML[name,title],mediaobjectelement}],
        And[type==="informal",title==="",
          description=!=""],{figuretag,mediaobjecttag,
            mediaobjectattributes}={"informalfigure","mediaobject",{}};
        parenttag=figuretag;
        XMLElement[parenttag,{"id"->name},{mediaobjectelement}],
        And[type==="inline",title==="",
          description=!=""],{mediaobjecttag,
            mediaobjectattributes}={"inlinemediaobject",{"id"->name}};
        parenttag=mediaobjecttag;mediaobjectelement,True,Abort[]];
    Export[StringJoin[name,"-graph.xml"],
      XMLObject[
          "Document"][{XMLObject["Declaration"]["Version"->"1.0",
            "Encoding"->"UTF-8"],
          XMLObject["Doctype"][parenttag,"Public"->publicId,
            "System"->systemId]},parentelement,{},"Valid"->True],"XML"]]

exTable[name_String,tbl:{{__}..},title_String]:=
  Export[name<>".xml",
    Module[{parenttag="table"},
      XMLObject[
          "Document"][{XMLObject["Declaration"]["Version"->"1.0",
            "Encoding"->"UTF-8"],
          XMLObject["Doctype"][parenttag,"Public"->publicId,
            "System"->systemId]},
        XMLElement[
          parenttag,{"id"->name},{XMLElement["title",{},{title}],
            Apply[XMLElement[
                  "tgroup",{"cols"\[Rule]
                      ToString@
                        Length@{##}},{##}]&,{XMLElement["thead",{},{##}]&@@
                      Take[#,1],
                    XMLElement["tbody",{},{##}]&@@Take[#,{2,-1}]}&@
                Apply[XMLElement["row",{},{##}]&,
                  MapIndexed[entryML[name,#1,#2]&,tbl,{2}],{1}]]}],{},
        "Valid"->True]],"XML"]

titleML[_String,""]:=Abort[]
titleML[name_String,RowBox[blist:{__}]]:=
    XMLElement["title",{},
      MapIndexed[content[name,#1,Sequence@@@{#2},None]&,blist]];
titleML[name_String,xpr_]:=content[name,xpr,{1},"title"];

entryML[_String,"",_List]:=XMLElement["entry",{},{}]
entryML[name_String,RowBox[blist:{__}],pspec_List]:=
    XMLElement["entry",{},
      MapIndexed[content[name,#1,Sequence@@@{pspec,#2},None]&,blist]];
entryML[name_String,xpr_,pspec_List]:=content[name,xpr,pspec,"entry"];
content[_String,xpr_/;MatchQ[Head[xpr],(String|literallayout)],{__Integer},
      elem_/;Or[Head[elem]===String,elem===None]]:=
    Module[{strxpr=xpr/.literallayout\[Rule]Function[#]},
      Function[If[elem=!=None,XMLElement[elem,{},{#}],#]]@
        Function[
            If[Head[xpr]===literallayout,
              XMLElement["literallayout",{},{#}],#]]@
          XML`RawXML[
            Apply[StringJoin,
              If[Or[33\[LessEqual]#\[LessEqual]127,#\[Equal]10],
                    FromCharacterCode[#],
                    System`Convert`XMLDump`\
generateNumericEntityFromCharacterCode@#]&/@
                ToCharacterCode[strxpr]](*StringReplace[
                XML`MathML`BoxesToMathML@
                  strxpr,{"<math xmlns='http://www.w3.org/1998/Math/MathML'>\n <mtext>"->
                    "","</mtext>\n</math>"->"",
                  "<math xmlns='http://www.w3.org/1998/Math/MathML'>\n <mi>"->
                    "","</mi>\n</math>"->""}]*)]];
content[_String,xpr_?NumberQ/;Accuracy[xpr]>0,{__Integer},
      elem_/;Or[Head[elem]===String,elem===None]]:=
    If[elem=!=None,XMLElement[elem,{},{#}],#]&@ExportString[xpr,"String"];
content[name_String,xpr_,pspec:{__Integer},
    elem_/;Or[Head[elem]===String,elem===None]]:=
  Module[{xmlnsurl="http://www.w3.org/2000/xmlns/",xiurl="http://www.w3.org/2001/XInclude",
      xpxpr="xpointer(//inlineequation)",hrefurl=
        exMath[name<>StringJoin@@Most@Flatten[{ToString[#],"_"}&/@pspec],xpr,"inline",
            "",ExportWithId\[Rule]False][[1]]},
    If[elem=!=None,XMLElement[elem,{},{#}],#]&@
      XMLElement[{xiurl,"include"},{{xmlnsurl,"xi"}\[Rule]xiurl,
          "href"\[Rule]hrefurl,"xpointer"\[Rule]xpxpr},{}]]

End[];

EndPackage[];
(*
MMADE, a Mathematica DocBook Exporter
The license and Copyright information for MMADE is included in rights.txt
in the XML directory.
*)